<?php

if( ! defined('DOCROOT') )
{
    require_once '/var/www/advancelab/chilik2/unittest/bootstrap.php';
}
class CatchRouterTest extends UnitTestCase
{
    function testingMatchEmpty()
    {
        $request = new Core\MVCRequest();
        $request->uri = '';
        
        $route = new Implementation\CatchAllRouter();
        $routeInfo = $route->matches( $request );
        
        $this->assertEqual(NULL, $routeInfo);
        
        /* $this->dump($route); */
    }
    
    function testingNoMatch1()
    {
        $request = new Core\MVCRequest();
        $request->uri = '/foo';
        
        $route = new Implementation\CatchAllRouter();
        $routeInfo = $route->matches( $request );
        
        $this->assertEqual(NULL, $routeInfo);
        

        /* $this->dump($route); */
    }
    
    function testingMatch1()
    {
        $request = new Core\MVCRequest();

        $request->uri = '/my/doaction';
        
        $route = new Implementation\CatchAllRouter();
        $routeInfo = $route->matches( $request );

        $this->assertEqual('/my/doaction', $routeInfo->matchedRoute);
        $this->assertEqual('myController', $routeInfo->controllerClass);
        $this->assertEqual('doaction', $routeInfo->action);
        
        /* $this->dump($routeInfo); */
    }
    
    function testingMatchChangedActionName()
    {
        $request = new Core\MVCRequest();
        $request->uri = '/mytwee/action';
        
        $route = new Implementation\CatchAllRouter();
        $routeInfo = $route->matches( $request );

        $this->assertEqual('/mytwee/action', $routeInfo->matchedRoute);
        $this->assertEqual('mytweeController', $routeInfo->controllerClass);
        $this->assertEqual('action', $routeInfo->action);
        
        /* $this->dump($routeInfo); */
    }
    
    function testingMatchWithParam()
    {
        $request = new Core\MVCRequest();
        $request->uri = '/my/doaction/value1';
        
        $route = new Implementation\CatchAllRouter();
        $routeInfo = $route->matches( $request );
        /* $this->dump($request->variables); */

        $this->assertEqual(array('param1' => 'value1' ), $request->variables);
        $this->assertEqual('/my/doaction/value1', $routeInfo->matchedRoute);
        $this->assertEqual('myController', $routeInfo->controllerClass);
        $this->assertEqual('doaction', $routeInfo->action);
        
        /* $this->dump($routeInfo); */
    }
    
    function testingMatchWithParams()
    {
        $request = new Core\MVCRequest();
        $request->uri = '/my/doaction/value1/value2';
        
        $route = new Implementation\CatchAllRouter();
        $routeInfo = $route->matches( $request );
        /* $this->dump($request->variables); */

        $this->assertEqual(array('param1' => 'value1', 'param2' => 'value2' ), $request->variables);
        $this->assertEqual('/my/doaction/value1/value2', $routeInfo->matchedRoute);
        $this->assertEqual('myController', $routeInfo->controllerClass);
        $this->assertEqual('doaction', $routeInfo->action);
        
        /* $this->dump($routeInfo); */
    }
    
    function testingMatchWithCustomParamName()
    {
        $request = new Core\MVCRequest();
        $request->uri = '/my/doaction/value1/value2';
        
        $route = new MyCatchAllRoute();
        $routeInfo = $route->matches( $request );
        /* $this->dump($request->variables); */

        $this->assertEqual(array('wibble' => 'value1', 'wobble' => 'value2' ), $request->variables);
        $this->assertEqual('/my/doaction/value1/value2', $routeInfo->matchedRoute);
        $this->assertEqual('myController', $routeInfo->controllerClass);
        $this->assertEqual('doaction', $routeInfo->action);
        
        /* $this->dump($routeInfo); */
    }
    
    function testingMatchWithCustomParamNameCustomActionName()
    {
        $request = new Core\MVCRequest();
        $request->uri = '/mytwee/action/value1/value2';
        
        $route = new MyCatchAllRoute();
        $routeInfo = $route->matches( $request );
        /* $this->dump($request->variables); */

        $this->assertEqual(array('wibble' => 'value1', 'wobble' => 'value2' ), $request->variables);
        $this->assertEqual('/mytwee/action/value1/value2', $routeInfo->matchedRoute);
        $this->assertEqual('mytweeController', $routeInfo->controllerClass);
        $this->assertEqual('action', $routeInfo->action);
        
        /* $this->dump($routeInfo); */
    }
    
    function testingMatchWithDifferentUriMatch()
    {
        $request = new Core\MVCRequest();
        $request->host = 'test.host';
        $request->uri = '/mytwee/action/value1/value2';
        $request->requestId = $request->host.$request->uri;
        
        $route = new MyCatchAllRouteForFullUri();
        $routeInfo = $route->matches( $request );
        /* $this->dump($request->variables); */

        $this->assertEqual(array('param1' => 'value1', 'param2' => 'value2' ), $request->variables);
        $this->assertEqual('test.host/mytwee/action/value1/value2', $routeInfo->matchedRoute);
        $this->assertEqual('mytweeController', $routeInfo->controllerClass);
        $this->assertEqual('action', $routeInfo->action);
        
        /* $this->dump($routeInfo); */
    }
}
